package com.api.interf;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

public class HelloWorld {
    public static void main(String[] args){
        HelloWorld helloWorld = new HelloWorld();
        helloWorld.doPost();
        doGet();
    }
    //@Test
    public void doPost(){
        //http://119.23.241.154:8080/futureloan/mvc/api/member/login?mobilepone=13517315120&pwd=123456
     //http请求
        String url = "https://tvip.nucarf.cn/PublicApi/Company/balance";
        String sign = "?sign=111&appid=9fa7c4692b238526d06283fc0fedcce8";
        HttpPost  httpPost = new HttpPost(url+sign);
        //准备接口地址


        try {
            //表单形式构建参数名和参数值
            //NameValuePair mobileponePair = new BasicNameValuePair("mobilephone","13517315120");
            //NameValuePair pwdPair = new BasicNameValuePair("pwd","123456");
            //List<NameValuePair>  params = new ArrayList<NameValuePair>();
            //params.add(mobileponePair);
            //params.add( pwdPair);
            //将参数封装在请求体中
            //httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
            //创建客户端
            CloseableHttpClient httpClient = HttpClients.createDefault();

            //发送接口请求,获取接口响应
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

            //获取响应数据，为json格式，转换为字符串
            HttpEntity resultJson = httpResponse.getEntity();
            //将json转换为字符串
            //System.out.println(resultJson);
            String resultString = EntityUtils.toString(resultJson);
            //System.out.println(resultString);
            JSONObject jsonObject=JSONObject.parseObject(resultString);
            System.out.println(jsonObject.toJSONString());

        } catch (Exception e) {
            e.printStackTrace();
        }
        //


    }
      public static void doGet(){
          String uri = "https://tvip.nucarf.cn/PublicApi/Company/balance?sign=111&appid=9fa7c4692b238526d06283fc0fedcce8";
          HttpGet httpGet = new HttpGet(uri);
          //准备接口地址

          try {
              CloseableHttpClient httpClient = HttpClients.createDefault();
              //发送接口请求,获取接口响应
              CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
              //System.out.println(httpResponse);

              //获取响应数据，为json格式，转换为字符串
              HttpEntity jsonResult = httpResponse.getEntity();
              //将json转换为字符串
              String resultString = EntityUtils.toString(jsonResult);
              JSONObject jsonObject=JSONObject.parseObject(resultString);
              System.out.println(jsonObject.toJSONString());


          } catch (Exception e) {
              e.printStackTrace();
          }
          //

      }


}
